package logic

import (
	"context"
	"order-srv/model/mysql"

	"order-srv/internal/svc"
	"order-srv/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type GenerateOrderGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGenerateOrderGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateOrderGoodsLogic {
	return &GenerateOrderGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GenerateOrderGoodsLogic) GenerateOrderGoods(in *order.GenerateOrderGoodsRequest) (*order.GenerateOrderGoodsResponse, error) {
	// todo: add your logic here and delete this line
	og := mysql.OrderGoods{
		UserId:       int32(in.UserId),
		OrderId:      int32(in.OrderId),
		GoodsId:      int32(in.GoodsId),
		GoodsName:    in.GoodsTitle,
		OrderGoodsSn: in.OrderGoodsSn,
		GoodsPrice:   in.GoodsPrice,
		GoodsNum:     int32(in.GoodsNum),
	}
	err := og.CreateOrderGoods()
	if err != nil {
		logx.Error("生成订单商品失败")
		return nil, err
	}
	return &order.GenerateOrderGoodsResponse{OrderGoodsId: int64(og.ID)}, nil
}
